package com.kye.express.business.jd.shop.viewbinder;

import android.databinding.DataBindingUtil;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.jakewharton.rxbinding2.view.RxView;
import com.kye.base.DebounceObservableTransformer;
import com.kye.express.R;
import com.kye.express.business.jd.shop.model.JDGoodsCategoryModel;
import com.kye.express.databinding.ItemJdCategoryBinding;

import io.reactivex.functions.Consumer;
import me.drakeet.multitype.ItemViewBinder;

/**
 * @author : Joe
 * @version : 1.0
 * @editor : Joe
 * @created : 29/01/2018
 * @updated : 29/01/2018
 * @description : <Description>
 * @update_reason : <UpdateReason>
 */

public class JDCategoryViewBinder extends ItemViewBinder<JDGoodsCategoryModel, JDCategoryViewBinder.ViewHolder> {

    private OnCategoryItemListener onItemListener;

    public JDCategoryViewBinder(OnCategoryItemListener onItemListener) {
        this.onItemListener = onItemListener;
    }

    @NonNull
    @Override
    protected ViewHolder onCreateViewHolder(@NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        View view = inflater.inflate(R.layout.item_jd_category, parent, false);
        return new ViewHolder(view);
    }

    @Override
    protected void onBindViewHolder(@NonNull ViewHolder holder, @NonNull JDGoodsCategoryModel item) {
        holder.bindData(item);
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        private ItemJdCategoryBinding binding;
        private JDGoodsCategoryModel item;
        private View view;

        public ViewHolder(View itemView) {
            super(itemView);
            view = itemView;
            binding = DataBindingUtil.bind(itemView);
            RxView.clicks(binding.category)
                    .compose(new DebounceObservableTransformer<>())
                    .subscribe(new Consumer<Object>() {
                        @Override
                        public void accept(Object o) throws Exception {
                            if (onItemListener != null) {
                                onItemListener.onCategoryItemClicked(item);
                            }
                        }
                    });
        }

        public void bindData(JDGoodsCategoryModel item) {
            this.item = item;
            if (item.getCategoryBean() == null) {
                binding.categoryImage.setImageResource(R.drawable.ic_jd_category_all);
                binding.categoryName.setText("全部分类");
            } else {
                Glide.with(view.getContext())
                        .load(item.getCategoryBean().getImagePath())
                        .apply(new RequestOptions().error(R.drawable.error).placeholder(R.drawable.background_about_empty))
                        .into(binding.categoryImage);
                binding.categoryName.setText(item.getCategoryBean().getClassName());
            }
        }
    }

    public interface OnCategoryItemListener {
        void onCategoryItemClicked(JDGoodsCategoryModel item);
    }
}
